package libs

import (
	"fmt"
	"log"
	"os"
)

func CreateDirectoryIfNotExist(dir string) {
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		if err := os.MkdirAll(dir, 0755); err != nil {
			log.Println(err)
		}
	}
}

func ReadFile(filename string) []byte {
	var data, err = os.ReadFile(filename)
	if err != nil {
		log.Println(fmt.Sprintf("read file:%s,", filename) + err.Error())
		return nil
	}
	return data
}

func WriteToHTML(filename, content string) error {
	// Create or open the HTML file
	file, err := os.Create(filename)
	if err != nil {
		return fmt.Errorf("failed to create file: %w", err)
	}
	defer file.Close()
	_, err = file.WriteString(content)
	if err != nil {
		return fmt.Errorf("failed to write to file: %w", err)
	}

	return nil
}
